package com.read.operationsapi.service;

import com.alibaba.fastjson.JSON;
import com.mc.read.common.exception.ServiceException;
import com.mc.read.common.util.OSSClientUtil;
import com.mc.read.dao.mapper.BookinformationMapper;
import com.mc.read.dao.mapper.BooktypeMapper;
import com.mc.read.dao.models.Bookinformation;
import com.mc.read.dao.models.BookinformationExample;
import com.mc.read.dao.models.Booktype;
import com.mc.read.vo.respose.CrawlerRespose;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author liuhao
 * @date 2019/6/17 14:58
 */
@Service
@Slf4j
public class CrawlerService {
    @Autowired
    BookinformationMapper bookinformationMapper;
    @Autowired
    BooktypeMapper booktypeMapper;

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 查询书籍是否存在 selectBookIsExist
     **/
    public CrawlerRespose selectBookIsExist(Bookinformation bookinformation) {
        log.info("查询书籍是否存在,参数为{}", JSON.toJSONString(bookinformation));
        CrawlerRespose baseRespose = new CrawlerRespose();
        if (ObjectUtils.isEmpty(bookinformation.getAuthor()) || ObjectUtils.isEmpty(bookinformation.getBookname())) {
            throw new ServiceException(203020, "书籍名字和作者不能为空", JSON.toJSONString(bookinformation));
        }
        BookinformationExample example = new BookinformationExample();
        example.createCriteria().andAuthorEqualTo(bookinformation.getAuthor()).andBooknameEqualTo(bookinformation.getBookname()).andDeletemarkEqualTo(0);
        List<Bookinformation> bookinformationList = bookinformationMapper.selectByExample(example);
        if (bookinformationList.size() > 0) {
            baseRespose.setIsExist(1);
        }
        baseRespose.setReturnString(" ");
        return baseRespose;
    }

    /**
     * @Author:LiuHao
     * @Date:15:24 2019/5/31
     * 爬虫添加书籍 addBookForCrawler
     **/
    public CrawlerRespose addBookForCrawler(Bookinformation bookinformation) {
        log.info("爬虫添加书籍,参数为{}", JSON.toJSONString(bookinformation));
        CrawlerRespose baseRespose = new CrawlerRespose();
        if (ObjectUtils.isEmpty(bookinformation.getAuthor()) || ObjectUtils.isEmpty(bookinformation.getBookname())) {
            throw new ServiceException(203020, "书籍名字和作者不能为空", JSON.toJSONString(bookinformation));
        }
        BookinformationExample example = new BookinformationExample();
        example.createCriteria().andAuthorEqualTo(bookinformation.getAuthor()).andBooknameEqualTo(bookinformation.getBookname()).andDeletemarkEqualTo(0);
        List<Bookinformation> bookinformationList = bookinformationMapper.selectByExample(example);
        if (bookinformationList.size() > 0) {
            throw new ServiceException(203020, "书籍已存在");
        }
        if (ObjectUtils.isEmpty(bookinformation.getMainlevelname())) {
            bookinformation.setMainlevelname("其他");
            bookinformation.setMainlevelid(10);
        } else {
            Booktype booktype=getBooktype(bookinformation.getMainlevelname());
            bookinformation.setMainlevelname(booktype.getBooktypename());
            bookinformation.setMainlevelid(booktype.getBooktypeid());
        }
        if(ObjectUtils.isEmpty(bookinformation.getCoverurl())){
            OSSClientUtil ossClientUtil=new OSSClientUtil();
            try {
                String url= ossClientUtil.uploadImgForUrl(bookinformation.getCoverurl());
                bookinformation.setCoverurl(url);
            } catch (ServiceException e) {
                e.printStackTrace();
                bookinformation.setCoverurl(null);
            }
        }

        bookinformationMapper.insertSelective(bookinformation);
        baseRespose.setReturnString(" ");
        return baseRespose;
    }


    public Booktype getBooktype(String mainLevelName) {
        Booktype booktype = new Booktype();
        booktype.setBooktypeid(10);
        booktype.setBooktypename("其他");
        List<Booktype> booktypeList = booktypeMapper.selectMainLevel();
        for (Booktype type : booktypeList
                ) {
            if(mainLevelName.contains(type.getBooktypename())){
                booktype.setBooktypeid(type.getBooktypeid());
                booktype.setBooktypename(type.getBooktypename());
                return booktype;
            }
        }

        return booktype;
    }


    public static void main(String[] args) {
        String str="历史军事";
        String str1="历史1";

        System.out.println(str.contains(str1));
    }

}
